﻿/*
 * Handles Weapons.
 * Author: Bachir
 * 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Homebound
{
    class Weapons
    {
        public enum WeaponType { None, Drill, Hammer, Laser };
        public WeaponType type;
        public Level level;
        public Player player;
        public Texture2D Drill, Hammer, Laser;
        public KeyboardState currentKbState, previousKbState;
        public Vector2 drillSize, hammerSize, laserSize, drillPosition, hammerPosition, gunPosition, laserPosition, drillOrigin, hammerOrigin, gunOrigin, laserOrigin;
        public double timer = 0;
        public float scale = 1.0f, drillInterval = 150, laserInterval = 250, hammerRotation = 0.0f, hammerInterval = 20;
        public int drillFrame = 0, laserFrame = 1, playerDirection = 1;
        public float drillRadius { get { return scale * drillSize.X / 2; } }
        public float hammerRadius { get { return scale * hammerSize.X / 2; } }
        public float laserRadius { get { return scale * laserSize.X / 2; } }
        public bool active = false, hammerHitting = false, hammerRaised = false, hammerRe = false;
        public Rectangle drillRect, hammerRect, gunRect, laserRect;

        // Damage dealt by the hammer
        int hammerDmg = 10;

        // Game Pad State
        public GamePadState currentGpState, previousGpState;

        // Constructor
        public Weapons(Level level, Player player)
        {
            type = WeaponType.None;
            this.level = level;
            this.player = player;
            Drill = level.Content.Load<Texture2D>("Sprites/Weapons/Drill");
            Hammer = level.Content.Load<Texture2D>("Sprites/Weapons/Hammer");
            Laser = level.Content.Load<Texture2D>("Sprites/Weapons/Laser");
            drillSize = new Vector2(48, 48);
            hammerSize = new Vector2(48, 48);
            laserSize = new Vector2(48, 48);
            drillPosition = this.player.Position;
            drillRect = new Rectangle(drillFrame * (int)drillSize.X, 0, (int)drillSize.X, (int)drillSize.Y);
            drillOrigin = new Vector2(drillRect.Width / 2, drillRect.Height / 2);
            hammerPosition = this.player.Position;
            hammerRect = new Rectangle(0, 0, (int)hammerSize.X, (int)hammerSize.Y);
            hammerOrigin = new Vector2(0, hammerRect.Height / 2);
        }

        // Update
        public void Update(GameTime gameTime)
        {
            previousKbState = currentKbState;
            currentKbState = Keyboard.GetState();
            //level.scrollingText.Add(new ScrollingText(level.scrollFont, "blank", new Vector2(0, 0), Color.White);

            // Update game pad state
            previousGpState = currentGpState;
            currentGpState = GamePad.GetState(PlayerIndex.One);

            if
            (
                (currentKbState.IsKeyDown(Keys.Tab) 
                && previousKbState.IsKeyUp(Keys.Tab))
                ||
                (currentGpState.IsButtonDown(Buttons.LeftTrigger)
                && previousGpState.IsButtonUp(Buttons.LeftTrigger))
            )
            {
                type++;
                if (type > WeaponType.Laser)
                    type = WeaponType.None;
                level.scrollingText.Add(new ScrollingText(level.scrollFont, type.ToString(), player.Position - new Vector2(level.cameraLeft, level.cameraTop), Color.Goldenrod));
            }
            if (currentKbState.IsKeyDown(Keys.E) || currentGpState.IsButtonDown(Buttons.RightTrigger)
                || currentGpState.IsButtonDown(Buttons.X))
            {
                active = true;
            }
            else
            {
                active = false;
                timer = 0;
            }
            if (player.Velocity.X < 0)
            {
                playerDirection = -1;
            }
            if (player.Velocity.X > 0)
            {
                playerDirection = 1;
            }

            switch (type)
            {
                case WeaponType.None:
                    //UpdateNone(gameTime);
                    break;

                case WeaponType.Drill:
                    UpdateDrill(gameTime);
                    break;

                case WeaponType.Hammer:
                    UpdateHammer(gameTime);
                    break;

                case WeaponType.Laser:
                    //UpdateLaser(gameTime);
                    break;
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            switch (type)
            {
                case WeaponType.None:
                    //DrawNone(spriteBatch);
                    break;
                case WeaponType.Drill:
                    DrawDrill(spriteBatch);
                    break;
                case WeaponType.Hammer:
                    DrawHammer(spriteBatch);
                    break;
                case WeaponType.Laser:
                    //DrawLaser(spriteBatch);
                    break;
            }
        }

        // Update the Drill tool
        public void UpdateDrill(GameTime gameTime)
        {
            int flip = playerDirection;
            drillPosition = player.Position + new Vector2(flip * 35, -30);

            if (active)
            {
                timer += gameTime.ElapsedGameTime.TotalMilliseconds;
                if (timer > drillInterval && drillFrame != 2)
                {
                    drillFrame++;
                    timer = 0;
                }
                if (timer > drillInterval && drillFrame == 2)
                {
                    drillFrame = 0;
                    timer = 0;
                }
            }
            drillRect.X = drillFrame * (int)drillSize.X;
        }

        public void DrawDrill(SpriteBatch spriteBatch)
        {
            SpriteEffects flip = SpriteEffects.None;
            if (playerDirection < 0)
                flip = SpriteEffects.FlipHorizontally;
            spriteBatch.Draw(Drill, drillPosition, drillRect, Color.White, 0f, drillOrigin, scale, flip, 0);
        }

        // Update the Hammer weapon
        public void UpdateHammer(GameTime gameTime)
        {
            // Point hammer in player direction
            int flip = playerDirection;
            hammerOrigin.X = 5;

            if (playerDirection < 0)
            {
                hammerOrigin.X = hammerSize.X - 5;
            }

            hammerPosition = player.Position + new Vector2(flip * 10, -20);

            // Update timer
            timer += gameTime.ElapsedGameTime.TotalMilliseconds;

            // Update hammer position when not being swung
            if (active && !hammerHitting && timer > hammerInterval)
            {
                hammerHitting = true;
            }

            // Update hammer being swung
            if (hammerHitting)
            {
                // Adjusted rectangle for collisions
                Rectangle offsetRect;

                // Update hammer Rectangle, based on player direction
                // Left
                if (playerDirection < 0)
                {

                    offsetRect = new Rectangle
                    (
                         (int)(hammerPosition.X + level.cameraLeft - 32 - 48),
                         (int)(hammerPosition.Y + (int)level.cameraTop),
                         hammerRect.Width,
                         hammerRect.Height
                     );

                }

                // Right
                else
                {
                    offsetRect = new Rectangle
                    (
                        (int)(hammerPosition.X + level.cameraLeft),
                        (int)(hammerPosition.Y + (int)level.cameraTop),
                        hammerRect.Width,
                        hammerRect.Height
                    );
                }

                // Update enemy rectangle
                Rectangle enemyRect;

                //Console.WriteLine("Hammer Pos" + offsetRect);
                //Console.WriteLine("E Pos" + enemyRect);

                // Check for collision with enemies
                foreach (Enemy e in level.enemies)
                {
                    enemyRect = new Rectangle
                            (
                                e.BoundingRectangle.X + (int)level.cameraLeft,
                                e.BoundingRectangle.Y + (int)level.cameraTop,
                                e.BoundingRectangle.Width,
                                e.BoundingRectangle.Height
                            );

                    if (offsetRect.Intersects(enemyRect))
                    {
                        e.takeDamage( hammerDmg );
                    }
                }

                if (!hammerRaised)
                {
                    hammerRotation -= flip * 0.10f;
                    if ((flip > 0 && hammerRotation < MathHelper.ToRadians(-75)) || (flip < 0 && hammerRotation > MathHelper.ToRadians(75)))
                    {
                        hammerRaised = true;
                    }
                }
                if (hammerRaised)
                {
                    if (!hammerRe)
                        hammerRotation += flip * 0.15f;
                    if ((flip > 0 && hammerRotation > MathHelper.ToRadians(45)) || (flip < 0 && hammerRotation < MathHelper.ToRadians(-45)))
                    {
                        hammerRe = true;
                    }
                    if (hammerRe)
                    {
                        hammerRotation -= flip * 0.05f;
                        if ((flip > 0 && hammerRotation < 0) || (flip < 0 && hammerRotation > 0))
                        {
                            hammerRaised = false;
                            hammerHitting = false;
                            hammerRe = false;
                            timer = 0;
                        }
                    }
                }
            }
        }

        public void DrawHammer(SpriteBatch spriteBatch)
        {
            SpriteEffects flip = SpriteEffects.None;
            if (playerDirection < 0)
                flip = SpriteEffects.FlipHorizontally;
            spriteBatch.Draw(Hammer, hammerPosition, hammerRect, Color.White, hammerRotation, hammerOrigin, scale, flip, 0);
        }
    }
}
